home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / stdcpp / filebuf.cc < prev    next >
C/C++ Source or Header  |  1996-11-05  |  6KB  |  224 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2.    Changed for emx by Eberhard Mattes -- Sep 1996
  3. Copyright (C) 1993, 1995 Free Software Foundation
  4.  
  5. This file is part of the GNU IO Library.  This library is free
  6. software; you can redistribute it and/or modify it under the
  7. terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this library; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. As a special exception, if you link this library with files
  21. compiled with a GNU compiler to produce an executable, this does not cause
  22. the resulting executable to be covered by the GNU General Public License.
  23. This exception does not however invalidate any other reasons why
  24. the executable file might be covered by the GNU General Public License.
  25.  
  26. Written by Per Bothner (bothner@cygnus.com). */
  27.  
  28. /* Modified by Klaus Gebhardt, 1995 */
  29.  
  30. #include "iostreamP.h"
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include <errno.h>
  35. #include "builtinbuf.h"
  36.  
  37. void filebuf::init()
  38. {
  39.   _IO_file_init(this);
  40. }
  41.  
  42. filebuf::filebuf()
  43. {
  44.   _IO_file_init(this);
  45. }
  46.  
  47. #if !_IO_UNIFIED_JUMPTABLES
  48. /* This is like "new filebuf()", but it uses the _IO_file_jump jumptable,
  49.    for eficiency. */
  50.  
  51. filebuf* filebuf::__new()
  52. {
  53.   filebuf *fb = new filebuf;
  54.   _IO_JUMPS(fb) = &_IO_file_jumps;
  55.   fb->_vtable() = builtinbuf_vtable;
  56.   return fb;
  57. }
  58. #endif
  59.  
  60. filebuf::filebuf(int fd)
  61. {
  62.   _IO_file_init(this);
  63.   _IO_file_attach(this, fd);
  64. }
  65.  
  66. filebuf::filebuf(int fd, char* p, int len)
  67. {
  68.   _IO_file_init(this);
  69.   _IO_file_attach(this, fd);
  70.   setbuf(p, len);
  71. }
  72.  
  73. filebuf::~filebuf()
  74. {
  75.   if (_IO_file_is_open(this))
  76.     {
  77.       _IO_do_flush (this);
  78.       if (!(xflags() & _IO_DELETE_DONT_CLOSE))
  79.     _IO_SYSCLOSE (this);
  80.     }
  81. }
  82.  
  83. filebuf* filebuf::open(const char *filename, ios::openmode mode, int prot)
  84. {
  85.   if (_IO_file_is_open (this))
  86.     return NULL;
  87.  
  88.   int posix_mode;
  89.   int read_write;
  90.  
  91.   if (mode & ios::app)
  92.     mode |= ios::out;
  93.  
  94.   if ((mode & (ios::in|ios::out)) == (ios::in|ios::out)) {
  95.     posix_mode = O_RDWR;
  96.     read_write = 0;
  97.   }
  98.   else if (mode & ios::out)
  99.     posix_mode = O_WRONLY, read_write = _IO_NO_READS;
  100.   else if (mode & (int)ios::in)
  101.     posix_mode = O_RDONLY, read_write = _IO_NO_WRITES;
  102.   else
  103.     posix_mode = 0, read_write = _IO_NO_READS+_IO_NO_WRITES;
  104.  
  105. #ifndef __EMX__
  106.   if (mode & ios::binary)
  107.     {
  108.       mode &= ~ios::binary;
  109. #ifdef O_BINARY
  110.       /* This is a (mis-)feature of DOS/Windows C libraries. */
  111.       posix_mode |= O_BINARY;
  112. #endif
  113.     }
  114. #endif
  115.  
  116.   if ((mode & (int)ios::trunc) || mode == (int)ios::out)
  117.     posix_mode |= O_TRUNC;
  118.   if (mode & ios::app)
  119.     posix_mode |= O_APPEND, read_write |= _IO_IS_APPENDING;
  120.   if (!(mode & (int)ios::nocreate) && !(mode & (int)ios::in))
  121.     posix_mode |= O_CREAT;
  122.   if (mode & (int)ios::noreplace)
  123.     posix_mode |= O_EXCL;
  124.  
  125. #ifdef __EMX__
  126.   if (mode & (int)ios::bin)  posix_mode |= O_BINARY;
  127.   else                       posix_mode |= O_TEXT;
  128. #endif
  129.  
  130.   int fd = ::open(filename, posix_mode, prot);
  131.   if (fd < 0)  return NULL;
  132.  
  133.   _fileno = fd;
  134.   xsetflags(read_write, _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
  135.   if (mode & (ios::ate|ios::app)) {
  136.     if (pubseekoff(0, ios::end) == EOF)
  137.       return NULL;
  138.   }
  139.  
  140.   _IO_link_in(this);
  141.   return this;
  142. }
  143.  
  144. filebuf* filebuf::open(const char *filename, const char *mode)
  145. {
  146.   return (filebuf*)_IO_file_fopen(this, filename, mode);
  147. }
  148.  
  149. filebuf* filebuf::attach(int fd)
  150. {
  151.   return (filebuf*)_IO_file_attach(this, fd);
  152. }
  153.  
  154. streambuf* filebuf::setbuf(char* p, int len)
  155. {
  156.   return (streambuf*)_IO_file_setbuf (this, p, len);
  157. }
  158.  
  159. int filebuf::doallocate() { return _IO_file_doallocate(this); }
  160.  
  161. int filebuf::overflow(int c)
  162. {
  163.   return _IO_file_overflow(this, c);
  164. }
  165.  
  166. int filebuf::underflow()
  167. {
  168.   return _IO_file_underflow(this);
  169. }
  170.  
  171. int filebuf::sync()
  172. {
  173.   return _IO_file_sync(this);
  174. }
  175.  
  176. streampos filebuf::seekoff(streamoff offset, _seek_dir dir, int mode)
  177. {
  178.   return _IO_file_seekoff (this, offset, dir, mode);
  179. }
  180.  
  181. filebuf* filebuf::close()
  182. {
  183.   return (_IO_file_close_it(this) ? (filebuf*)NULL : this);
  184. }
  185.  
  186. streamsize filebuf::sys_read(char* buf, streamsize size)
  187. {
  188.   return _IO_file_read(this, buf, size);
  189. }
  190.  
  191. streampos filebuf::sys_seek(streamoff offset, _seek_dir dir)
  192. {
  193.   return _IO_file_seek(this, offset, dir);
  194. }
  195.  
  196. streamsize filebuf::sys_write(const char *buf, streamsize n)
  197. {
  198.   return _IO_file_write (this, buf, n);
  199. }
  200.  
  201. int filebuf::sys_stat(void* st)
  202. {
  203.   return _IO_file_stat (this, st);
  204. }
  205.  
  206. int filebuf::sys_close()
  207. {
  208.   return _IO_file_close (this);
  209. }
  210.  
  211. streamsize filebuf::xsputn(const char *s, streamsize n)
  212. {
  213.   return _IO_file_xsputn(this, s, n);
  214. }
  215.  
  216. streamsize filebuf::xsgetn(char *s, streamsize n)
  217. {
  218.     // FIXME: OPTIMIZE THIS (specifically, when unbuffered()).
  219.     return streambuf::xsgetn(s, n);
  220. }
  221.  
  222. // Non-ANSI AT&T-ism:  Default open protection.
  223. const int filebuf::openprot = 0644;
  224.